#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <assert.h>
#include <sys/socket.h>

#define TTL 64
#define MAXSIZE 512
#define STRING "now the time is 2022.11.2"

int main(int argc, char **argv) {
    if (argc != 3) {
        printf("usage %s <mulicast_ip> <mulitcast_port>", argv[0]);
        return 0;
    }
    struct sockaddr_in mul_addr;
    int sockfd = socket(PF_INET, SOCK_DGRAM, 0);
    assert(sockfd != -1);
    memset(&mul_addr, 0, sizeof(mul_addr));
    mul_addr.sin_family = AF_INET;
    mul_addr.sin_addr.s_addr = inet_addr(argv[1]);
    mul_addr.sin_port = htons(atoi(argv[2]));
    
    char sendbuf[MAXSIZE] = {0};
    memcpy(sendbuf, STRING, sizeof(STRING));

    int ttl = TTL;
    int rc = setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, (void *)&ttl, sizeof(ttl));
    assert(rc == 0);

    printf("sendbuf: %s\n", sendbuf);
    sendto(sockfd, sendbuf, strlen(sendbuf), 0, (struct sockaddr *)&mul_addr, sizeof(mul_addr));
    sleep(1);
 
    close(sockfd);
    return 0;
}